package com.alstudio.utils.xml;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.text.TextUtils;

import com.alstudio.utils.xml.BaseXmlParser.XmlParseCallback;

/**
 * 
 * @author alonso lee
 *
 */
public abstract class XmlParseEventHandler implements XmlParseCallback {

	public abstract void processStartTag(String tag);

	public abstract void processEndTag(String tag);

	public abstract void processStartDocument();

	public abstract void processEndDocument();

	private BaseXmlParser mParser;

	public XmlParseEventHandler(BaseXmlParser parser) {
		// TODO Auto-generated constructor stub
		this.mParser = parser;
	}

	public String processText(String tag) {
		String text = null;
		try {
			text = mParser.readText();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return text;
	}

	public String getAttValue(String att) {
		if (TextUtils.isEmpty(att)) {
			throw new IllegalArgumentException("att should not null");
		}
		String value = "";
		value = mParser.getAttValue(att);

		return value;
	}

	/**
	 * 获取当前节点的所有属性
	 * @param tag
	 * @return
	 */
	public ArrayList<String> getAttributes(String tag) {
		ArrayList<String> keys = null;
		try {
			keys = mParser.getAttributes(tag);
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return keys;
	}

	public void startParse() throws XmlPullParserException, IOException {
		mParser.startParse();
	}

	@Override
	public void onTagStart(String tag) {
		// TODO Auto-generated method stub
		processStartTag(tag);
	}

	@Override
	public void onTagEnd(String tag) {
		// TODO Auto-generated method stub
		processEndTag(tag);
	}

	@Override
	public void onDocumentStart() {
		// TODO Auto-generated method stub
		processStartDocument();
	}

	@Override
	public void onDocumentEnd() {
		// TODO Auto-generated method stub
		processEndDocument();
	}

}
